/*
 * $Author: chyjm $
 * $Date: 2008-09-23 23:40:55 -0400 (Tue, 23 Sep 2008) $
 * $Revision: 168 $
 */


package com.jchy.graph.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.awt.Color;

import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JColorChooser;

import org.apache.log4j.*;

/**
 * GraphIt application for rendering graphs with vertices and edges.
 * 
 */
public class GraphIt extends JFrame {

	private static final long serialVersionUID = 999;

	private JPanel thePanel;

	/* GraphItPanel Components */
	private MainScreenLabel mainScreen;
	private JComboBox leftClickMode;
	private BorderLayout bl;
	private String[] leftClickModeOptions = {"Add Vertex", 
			                                 "Remove Vertex By Closest", 
			                                 "Add Edges By Closest", 
			                                 "Remove Edges By Closest", 
			                                 "Select Vertices By Box", 
			                                 "Select Vertices By Closest", 
			                                 "Select Vertices Connected to Closest", 
			                                 "Select Components With Same Size as Closest", 
			                                 "Move Selected Vertices", 
			                                 "Move Closest Vertex",
			                                 "Move Vertices Connected to Closest",
			                                 "Move Vertices Connected to Closest Same Color",
			                                 "Sort Vertices From Closest",
			                                 "Color Closest Vertex"};
	private Color currentColor;
	
	private static Logger log;
	
	public GraphIt()
    {
        log = Logger.getLogger(GraphIt.class);
		currentColor = Color.BLACK;
		setTitle("Graph It Application");
        thePanel = new JPanel();
        
		mainScreen = new MainScreenLabel();
		leftClickMode = new JComboBox(leftClickModeOptions);
		leftClickMode.setMaximumRowCount(leftClickModeOptions.length);
		leftClickMode.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				log.debug("ActionPerformed");
				log.debug(e);
				mainScreen.setMode((String)leftClickMode.getSelectedItem());
			}
             
		});
		bl = new BorderLayout();
		thePanel.setLayout(bl);
		leftClickMode.setActionCommand("leftclickmode");
		thePanel.add(leftClickMode, BorderLayout.NORTH);
		thePanel.add(mainScreen, BorderLayout.CENTER);
		
        getContentPane().add(thePanel);

		addWindowListener(new WindowAdapter() 
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});

		buildMenu();
		pack();

    }
    
	private void buildMenu()
	{
		// Instantiates JMenuBar, JMenu,
		// and JMenuItem.
		JMenuBar mb = new JMenuBar();
		JMenu filemnu = new JMenu("File");
		JMenuItem openitm = new JMenuItem("Open");
		JMenuItem saveitm = new JMenuItem("Save");
		JMenuItem saveasitm = new JMenuItem("Save As...");
		JMenuItem exititm = new JMenuItem("Exit");
		
		JMenu colormnu = new JMenu("Color");
		JMenuItem selcoloritm = new JMenuItem("Select Color");
		
		JMenu selectmnu = new JMenu("Selected");
		JMenuItem colorselvertsitm = new JMenuItem("Color Selected Vertices");
		JMenuItem seldeselitm = new JMenuItem("De-Select All");
		
		//Adds the item to the menu object
		filemnu.add(openitm);
		filemnu.add(saveitm);
		filemnu.add(saveasitm);
		filemnu.add(exititm);
		
		colormnu.add(selcoloritm);

		selectmnu.add(colorselvertsitm);
		selectmnu.add(seldeselitm);
		
		
		//Adds the menu object with item
		//onto the menu bar
		mb.add(filemnu); 
		mb.add(colormnu);
		mb.add(selectmnu);
		//Sets the menu bar in the frame
		setJMenuBar(mb);

		//Closes the application from the Exit 
		//menu item.
		exititm.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
             
		});// Ends method

        openitm.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				log.debug("Open ActionPerformed");
				log.debug(e);

				JFileChooser fc = new JFileChooser();
				int result = fc.showOpenDialog(mainScreen.getParent());
		        if (result == JFileChooser.APPROVE_OPTION) {
		            File file = fc.getSelectedFile();
		            mainScreen.open(file.getAbsolutePath());
		        }
		            
			}
             
		});
 
        saveitm.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				log.debug("Save ActionPerformed");
				log.debug(e);
				mainScreen.save();
			}
             
		});
 
        saveasitm.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				log.debug("Save As ActionPerformed");
				log.debug(e);

				JFileChooser fc = new JFileChooser();
				int result = fc.showSaveDialog(mainScreen.getParent());
		        if (result == JFileChooser.APPROVE_OPTION) {
		            File file = fc.getSelectedFile();
		            mainScreen.saveAs(file.getAbsolutePath());
		        }
			
			
			}
             
		});

        selcoloritm.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				log.debug("Color ActionPerformed");
				log.debug(e);
                
				currentColor = JColorChooser.showDialog(mainScreen, "Select Color", currentColor);
				mainScreen.getGraphItData().setCurrentColor(currentColor);
							
			}
             
		});

        colorselvertsitm.addActionListener(new ActionListener()
        {
        	public void actionPerformed(ActionEvent e)
        	{
        		mainScreen.getGraphItData().colorSelectedVertices(currentColor);
        		mainScreen.repaint();
        	}
        });
        
        seldeselitm.addActionListener(new ActionListener()
        {
        	public void actionPerformed(ActionEvent e)
        	{
        		mainScreen.getGraphItData().deSelectAll();
        		mainScreen.repaint();
        	}
        });
	}

    public static void main(String[] args)
    {
        GraphIt frame = new GraphIt();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(684,684);
        frame.setVisible(true);
    }

}
